import axios from 'axios'
import {globalConfig} from './config.js'


// 创建axios的对象
const instance = axios.create({
    baseURL: globalConfig.baseUrl,  //配置固定域名
    timeout: 5000
})

// 请求拦截
// 所有的网络请求都会走这个方法,可以在请求添加自定义内容
instance.interceptors.request.use(
    function (config) {
        // config.headers.token = '123456' // 请求头添加token值
        // config.headers.info = 'lxy'  //请求头添加info值
        return config
    },
    function (err) {
        return Promise.reject(err)
    }
)

instance.interceptors.response.use(
    function (response) {
        return response;
    },
    function (error) {
        switch(error.response.status){
            case 503:
                ElMessage({
                    message: error.response.data.message?error.response.data.message:'503 Service Unavailable',
                    type: 'error',
                })
                break;
            case 500:
                ElMessage({
                    message: error.response.data.message?error.response.data.message:'500 Internal server error',
                    type: 'error',
                })
                break;
            default:
                ElMessage({
                    message: '连接错误',
                    type: 'error',
                })
        }
        return Promise.reject(error);
    }
)
export function get(url, params) {
    return instance.get(url, {params: params})
}

export function post(url, data) {
    return instance.post(url, data)
}

export default instance;
